//
//  NSDate+Additions.m
//  YunDiTrip
//
//  Created by yyl on 16/4/7.
//  Copyright © 2016年 shenzhen yundi technology co.,Ltd. All rights reserved.
//

#import "NSDate+Additions.h"


@implementation NSDate (Additions)

+ (NSDate *)dateFromInterval:(NSTimeInterval)timeInterval {
    if (timeInterval <= 0) {
        return [NSDate date];
    } else {
        return [NSDate dateWithTimeIntervalSince1970:timeInterval];
    }
}

+ (NSString *)dateStrFromInterval:(NSTimeInterval)timeInterval {
    
    NSDate *date = [NSDate dateFromInterval:timeInterval];
    
    return [NSDate formatDate:date withFormat:kYmdHms];
}

+ (NSString *)dateStrFromInterval:(NSTimeInterval)timeInterval withFormat:(NSString *)format {
    
    NSDate *date = [NSDate dateFromInterval:timeInterval];
    
    return [NSDate formatDate:date withFormat:format];
}

+ (NSString *)formatDate:(NSDate *)date withFormat:(NSString*)format {
    if (date) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:format];
        NSString *currentDateStr = [dateFormatter stringFromDate:date];
        return currentDateStr;
    } else {
        return nil;
    }
}

+ (NSDate *)dateFromStr:(NSString *)str withFormat:(NSString*)format { 
    if (str.length <= 0) {
        return [NSDate date];
    } else {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:format];
        NSDate *currentDate = [dateFormatter dateFromString:str];
        return currentDate;
    }
}

- (NSDate *)dateNextDay:(NSInteger)spaceDay {
    NSDate *newDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:([self timeIntervalSinceReferenceDate] + spaceDay * 24*3600)];
    return newDate;
}


+ (NSString *)formatYTTWithDateComponents:(NSDateComponents *)components {
    
    if (!components) {
        //默认为当前时间
        //components = [[NSDateComponents alloc] init];
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
        components = [calendar components:unitFlags fromDate:[NSDate date]];
    }
    
    NSString *yyt = @"";
    NSTimeInterval secondsPerDay = 24 * 60 * 60;
    NSDate *today = [NSDate date];
    NSDate *tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
    NSDate *yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
    
    NSString * todayString = [[today description] substringToIndex:10];
    NSString * tomorrowString = [[tomorrow description] substringToIndex:10];
    NSString * yesterdayString = [[yesterday description] substringToIndex:10];
    
    NSString * dateString = [NSString stringWithFormat:@"%04ld-%02ld-%02ld", (long)components.year, (long)components.month, (long)components.day];
    
    if ([dateString isEqualToString:todayString]) {
        yyt = @"(今天)";
    } else if ([dateString isEqualToString:yesterdayString]) {
        yyt = @"(昨天)";
    }else if ([dateString isEqualToString:tomorrowString]) {
        yyt = @"(明天)";
    }
    
    NSString *dateStr = [NSString stringWithFormat:@"%04ld-%02ld-%02ld%@", (long)components.year, (long)components.month, (long)components.day, yyt];
    return dateStr;
}


 // 判断是否是今天
+ (BOOL )compareDate:(NSDate *)date{
    
    NSTimeInterval secondsPerDay = 24 * 60 * 60;
    NSDate *today = [[NSDate alloc] init];
    NSDate *tomorrow, *yesterday;
    
    tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
    yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
    
    // 10 first characters of description is the calendar date:
    NSString * todayString = [[today description] substringToIndex:10];
    NSString * yesterdayString = [[yesterday description] substringToIndex:10];
    NSString * tomorrowString = [[tomorrow description] substringToIndex:10];
    
    NSString * dateString = [[date description] substringToIndex:10];
    
    if ([dateString isEqualToString:todayString])
    {
        return YES;   // 今天
    } else if ([dateString isEqualToString:yesterdayString])
    {
        return NO; // 昨天
    }else if ([dateString isEqualToString:tomorrowString])
    {
        return NO;  // 明天
    }
    else
    {
        return NO;
    }
}
+ (NSString *)formatYTTWithDate:(NSDate *)date {
 
//    NSDateComponents *components = [[NSDateComponents alloc] init];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *components = [calendar components:unitFlags fromDate:date];
    
    return [NSDate formatYTTWithDateComponents:components];
    
}

@end
